<template>
  <div>
    <yt-crud
      v-bind="options"
      ref="crudRef"
      @onLoad="getData"
      :search-props="{
        showKeyword: true,
        keywordPlaceholder: t('business.refund.placeholder.keyword'),
        keywordWidth: 300,
      }"
      :fun-props="{
        exportBtn: false,
        delBtn: false,
        customForm: true,
      }"
      :table-props="{
        selection: false,
        delBtn: false,
        viewBtn: true,
        editBtn: false,
        menuSlot: true,
        menuWidth: 200,
      }"
      :loading="state.loading"
      :total="state.total"
      v-model:page="state.page"
      v-model:query="state.query"
      @handle-add="handleAdd"
      @change="onChange"
    >
      <template #rightToolbar></template>
      <template #menuSlot="scope:any">
        <div class="menu-btn">
          <el-tooltip class="box-item" effect="dark" :content="t('business.refundBalance.tooltip.balanceList')" placement="top">
            <el-button link type="primary" icon="grid" @click="handleBalanceList(scope.row)" />
          </el-tooltip>
          <div class="el-divider el-divider--vertical" role="separator"></div>
        </div>
        <template v-if="scope.row.state == 'Pending'">
          <el-tooltip class="box-item" effect="dark" :content="t('business.refund.action.refund')" placement="top">
            <div class="menu-btn" v-if="scope.row.state === 'Pending'">
              <el-button link type="primary" icon="Coin" @click="handleRefund(scope.row)" />
              <div class="el-divider el-divider--vertical"></div>
            </div>
          </el-tooltip>
        </template>

        <template v-if="scope.row.state == 'Pending'">
          <el-tooltip class="box-item" effect="dark" :content="t('crud.deleteText')" placement="top">
            <div class="menu-btn" v-if="scope.row.state === 'Pending'">
              <el-popconfirm :title="t('crud.confirmDeleteTooltip')" @confirm="handleRemove(scope.row)">
                <template #reference>
                  <el-button link type="danger" icon="Delete" />
                </template>
              </el-popconfirm>
              <div class="el-divider el-divider--vertical"></div>
            </div>
          </el-tooltip>
        </template>
      </template>
      <template #state="scope:any">
        <DictTag :options="RefundStateEnum" :value="scope.row.state" />
      </template>
      <template #customerId="scope:any">
        {{scope.row.userName}}
      </template>
    </yt-crud>
    <BalanceList ref="balanceListRef" @success="handleSuccess" />
    <RefundForm ref="formRef" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
import { IColumn } from '@/components/common/types/tableCommon'
import {refund, remove, list} from '@/api/business/refund'

import YtCrud from '@/components/common/yt-crud.vue'
import {ComponentInternalInstance} from 'vue'
import {useI18n} from 'vue-i18n'

import RefundForm from './modules/Form.vue'
import BalanceList from './modules/BalanceList.vue'
import {ElPopconfirm} from 'element-plus'


const { t } = useI18n()
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { RefundStateEnum } = toRefs<any>(proxy?.useEnum('refund_state_enum'))

const formRef = ref()
const balanceListRef = ref()

const state = reactive({
  page: {
    pageSize: 30,
    pageNum: 1,
  },
  total: 0,
  loading: false,
  query: {},
})



const column = ref<IColumn[]>([
  {
    label: t('business.refund.customerId'),
    key: 'customerId',
    search: false,
    formHide: false,
    colSpan: 22,
    slot: true,
  },
  {
    label: t('business.refund.tranId'),
    key: 'tranId',
    search: false,
    formHide: false,
    colSpan: 22,
    slot: false,
  },
  {
    label: t('business.refund.amount'),
    key: 'amount',
    type: 'number',
    search: false,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.refund.state'),
    key: 'state',
    type: 'select',
    componentProps: {
      options: RefundStateEnum,
    },
    search: true,
    formHide: true,
    slot: true,
  },
  {
    label: t('crud.table.column.createTime'),
    key: 'createTime',
    tableWidth: 180,
    sortable: true,
    type: 'date',
    formHide: true,
  },
])

const crudRef = ref()
const data = ref<any[]>([])
const getData = () => {
  state.loading = true
  list({
    ...state.page,
    ...state.query,
  })
    .then((res) => {
      data.value = res.data.rows
      state.total = res.data.total
    })
    .finally(() => {
      state.loading = false
    })
}

//价格添加
const handleAdd = async () => {
  formRef.value.openDialog()
}


const getDict = () => {}
getDict()


const handleSuccess = () => {
  getData()
}

const handleRefund = async (row: any) => {
  await proxy?.$modal.confirm(t('business.refund.tooltip.confirm'))
  refund(row.id).then(() => {
    ElMessage.success(t('operate.tooltip.success'))
    getData()
  }).finally(() => {
    proxy?.$modal.closeLoading()
  })
}

const handleRemove = (row: any) => {
  remove(row.id).then(()=>{
    ElMessage.success(t('operate.tooltip.success'))
    getData()
  }).finally(()=>{

  })
}

const handleBalanceList = (row: any) => {
  balanceListRef.value.openDialog(row)
}

const multipleSelection = ref<{ deviceId: string }[]>([])

const onChange = (data) => {
  multipleSelection.value = data
}

const options = reactive({
  ref: 'crudRef',
  data,
  column,
})
</script>

<style lang="scss" scoped></style>
